$().ready(function() {
	validateRule();
	loadtable();
	loadSelect();
	laydate({
		elem : '#paymentdate'
	});
	getnumber();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/finance/paymentform/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			money : {
				required : true
			},
			coupon : {
				required : true,
				number:true
			},
			payment : {
				required : true,
				number:true
			},
			paymentdate : {
				required : true
			}
		},
		messages : {
			money : {
				required : icon + "请输入金额"
			},
			coupon : {
				required : icon + "请输入金额",
				number:icon + "请正确输入金额"
			},
			payment : {
				required : icon + "请输入金额",
				number:icon + "请正确输入金额"
			},
			paymentdate : {
				required : icon + "请输入日期"
			}
		}
	})
}

function loadSelect() {
	$.ajax({
		cache : true,
		url : "/finance/role/list3",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			var $eperson = $("#eperson");
			for(var i=0;i<data.length;i++){
				var item = data[i];
				$eperson.append("<option value="+item.id+">"+item.rolename+"</option>")
			}
		}
	});
}


function loadtable() {
	$.ajax({
		cache : true,
		url : "/fin/table/list1",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			var $account = $("#account");
			for(var i=0;i<data.length;i++){
				var item = data[i];
				$account.append("<option value="+item.id+">"+item.clearingform+"</option>")
			}
		}
	});
}


var useys = function(){
	layer.open({
		type:2,
		title:"选择供应商",
		area : [ '700px', '500px' ],
		content:"/fin/ct/table1"
	})
}

//生成随机编号
function randomWord(randomFlag, min, max){
	var str = "",
		range = min,
		arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

		/*arr1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];*/

	// 随机产生
	if(randomFlag){
		range = Math.round(Math.random() * (max-min)) + min;
	}
	for(var i=0; i<range; i++){
		pos = Math.round(Math.random() * (arr.length-1));
		str += (arr[pos]);
	}
	return "FK"+str;
}

function getnumber() {
	var number = randomWord(false,13);
	$("#number").val(number);
}